package com.homedo.odin.solution.service.bizservice.mng;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.BrandFirmsSkuListValidRequestBean;
import com.homedo.odin.solution.contract.pc.BrandFirmsSkuPageResponseBean;
import com.homedo.odin.solution.enumcode.BrandStatusEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.dto.ProductListResponseBean;
import com.homedo.odin.solution.model.dto.UserSolutionCount;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.entity.SolutionSku;
import com.homedo.odin.solution.model.request.pc.brand.BrandFirmEditRequestBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandFirmDetailResponseBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandFirmPcListResponseBean;
import com.homedo.odin.solution.service.BrandFirmServiceImpl;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.SolutionSkuServiceImpl;
import com.homedo.odin.solution.service.common.HomedoService;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhanglei
 * @date: 2018/4/16
 * @Description:
 */
@Service
public class MngBrandFirmsService {

    @Autowired
    private BrandFirmServiceImpl brandFirmService;
    @Autowired
    private SolutionServiceImpl solutionService;
    @Autowired
    private SolutionSkuServiceImpl solutionSkuService;
    @Autowired
    private HomedoService homedoService;
    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    public BrandFirmsSkuPageResponseBean getBrands(BrandFirmsSkuListValidRequestBean requestBean) {
        Integer uid = requestBean.getUid();
        BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(uid);
        if (brandFirm == null || brandFirm.getStatus().intValue() != SolutionStateEnum.AUDITSUCCESS.getDbValue()) {
            throw new BizException(Meta.CODE_451.getCode(), "该品牌厂商不存在");
        }

        PageBean pageBean = new PageBean(requestBean.getCurrentPage(), requestBean.getPageSize());
        PageInfo<SolutionSku> pageInfo = solutionSkuService.querySolutionSkuByUserId(uid, pageBean);
        List<SolutionSku> queryList = pageInfo.getList();

        if (CollectionUtils.isEmpty(queryList)) {
            return null;
        }

        String joinSkuId = queryList.parallelStream().map(solutionSku -> solutionSku.getSkuId())
                .collect(Collectors.joining(","));

        List<ProductListResponseBean> responseBeanList = homedoService.getProductsZB(joinSkuId);
        BrandFirmsSkuPageResponseBean pageResponseBean = new BrandFirmsSkuPageResponseBean();
        pageResponseBean.setPageNum(pageInfo.getPageNum());
        pageResponseBean.setPages(pageInfo.getPages());
        pageResponseBean.setTotal(pageInfo.getTotal());
        pageResponseBean.setPageSize(pageInfo.getPageSize());
        pageResponseBean.setList(responseBeanList);

        return pageResponseBean;
    }

    public List<BrandFirmPcListResponseBean> getAllBrandFirm() {
        return brandFirmService.getAllBrandFirm();
    }

    public BrandFirmDetailResponseBean getBrandFirmByUserId(Integer userId, Integer loginUserId) {
        return convert2BrandFirmDetailResponseBean(brandFirmService.getBrandFirmByUserId(userId), loginUserId);
    }

    public BrandFirmDetailResponseBean getBrandFirmDetail(Integer brandId, Integer loginUserId) {
        List<BrandFirm> firms = brandFirmService.getBrandFirmByBrandId(brandId);
        if (null == firms || firms.size() <= 0) {
            throw new BizException("该品牌厂商不存在!");
        }
        BrandFirm brandFirm = firms.get(0);
        return convert2BrandFirmDetailResponseBean(brandFirm, loginUserId);
    }

    private BrandFirmDetailResponseBean convert2BrandFirmDetailResponseBean(BrandFirm brandFirm, Integer loginUserId) {
        if (brandFirm == null || BrandStatusEnum.AUDITSUCCESS.getKey() != brandFirm.getStatus().intValue()) {
            throw new BizException("该品牌厂商不存在!");
        }

        BrandFirmDetailResponseBean brandFirmDetail = new BrandFirmDetailResponseBean();
        BeanUtils.copyProperties(brandFirm, brandFirmDetail);

        BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(brandFirm.getBrandId());
        if (brandOwner != null) {
            brandFirmDetail.setBrandName(brandOwner.getBrandName());
            brandFirmDetail.setBrandIcon(brandOwner.getBrandIcon());
        }

        if (NumberHelper.validatePositive(loginUserId) && brandFirm.getUserId().equals(loginUserId)) {
            UserSolutionCount count = solutionService.queryUserSolutionCount(brandFirm.getUserId(), null,
                    SolutionTypeEnum.BRAND_SOLUTION.getDbValue());
            if (count != null) {
                brandFirmDetail.setSolutionTotal(count.getTotal());
                brandFirmDetail.setSolutionViewTotal(count.getViewTotal() == null ? 0 : count.getViewTotal());
                brandFirmDetail
                        .setSolutionDownloadTotal(count.getDownloadTotal() == null ? 0 : count.getDownloadTotal());
            }
        } else {
            UserSolutionCount count = solutionService.queryUserSolutionCount(brandFirm.getUserId(), 30,
                    SolutionTypeEnum.BRAND_SOLUTION.getDbValue());
            if (count != null && count.getTotal() > 0) {
                brandFirmDetail.setSolutionTotal(count.getTotal());
                brandFirmDetail.setSolutionViewTotal(count.getViewTotal() == null ? 0 : count.getViewTotal());
                brandFirmDetail
                        .setSolutionDownloadTotal(count.getDownloadTotal() == null ? 0 : count.getDownloadTotal());
            }
        }

        return brandFirmDetail;
    }

    public void editBrandFirm(BrandFirmEditRequestBean brandFirmEditBean) {
        BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(brandFirmEditBean.getUserId());
        if (brandFirm == null) {
            throw new BizException("该品牌厂商不存在!");
        }
        if (brandFirm.getStatus().intValue() != BrandStatusEnum.AUDITSUCCESS.getKey()) {
            throw new BizException("该品牌厂商正在审核中!");
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getFirmBanner())) {
            brandFirm.setFirmBanner(brandFirmEditBean.getFirmBanner());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getFirmHonor())) {
            brandFirm.setFirmHonor(brandFirmEditBean.getFirmHonor());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getFirmIntroduction())) {
            brandFirm.setFirmIntroduction(brandFirmEditBean.getFirmIntroduction());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getFirmIcon())) {
            brandFirm.setFirmIcon(brandFirmEditBean.getFirmIcon());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getContact())) {
            brandFirm.setContact(brandFirmEditBean.getContact());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getContactMobile())) {
            brandFirm.setContactMobile(brandFirmEditBean.getContactMobile());
        }
        if (StringUtils.isNotBlank(brandFirmEditBean.getContactEmail())) {
            brandFirm.setContactEmail(brandFirmEditBean.getContactEmail());
        }
        brandFirmService.updateById(brandFirm);
    }
}
